﻿using System;
using System.Windows;
using System.Windows.Controls;
using Phidgets;
using Phidgets.Events;
using ErrorEventArgs = Phidgets.Events.ErrorEventArgs;
using ErrorEventHandler = Phidgets.Events.ErrorEventHandler;

namespace APC_Application.Pages
{
    /// <summary>
    /// Interaction logic for MainPage.xaml
    /// </summary>
    public partial class MainPage : UserControl, ISwitchable
    {
        private InterfaceKit ifk;
        private int IR = 6;
        private int FORCE = 7;
        private int value_ir = 0;
        private int value_force = 0;
        private bool isSitting = false;

        public MainPage()
        {
            InitializeComponent();
            
            ifk = new InterfaceKit();
            ifk.Attach += new AttachEventHandler(ifk_Attach);
            ifk.Detach += new DetachEventHandler(ifk_Detach);
            ifk.Error += new ErrorEventHandler(ifk_Error);

            ifk.SensorChange += new SensorChangeEventHandler(ifk_SensorChange);

            try
            {
                ifk.open();
            }
            catch (Exception)
            {
                throw;
            }
            
            int sensors = ifk.sensors.Count;
        }

        private void ifk_Attach(object sender, AttachEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate()
            {
                try
                {
                    InterfaceKit attached = (InterfaceKit) sender;
                    int sensors = attached.sensors.Count;
                }
                catch (Exception)
                {

                    throw;
                }
            }));


        }

        void ifk_Detach(object sender, DetachEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate()
            {
                InterfaceKit detached = (InterfaceKit)sender;
            }));
        }

        void ifk_Error(object sender, ErrorEventArgs e)
        {
            //MessageBox.Show(e.Code + ": " + e.Type + System.Environment.NewLine + e.Description, "Phidget Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        void ifk_SensorChange(object sender, SensorChangeEventArgs e)
        {
            if (e.Index == IR)
            {
                value_ir = e.Value;
                Console.WriteLine(value_ir + " isSitting:"+isSitting);
                if (value_ir < 930 && isSitting)
                {
                    this.Dispatcher.Invoke(new Action(delegate()
                    {
                        ConnectionLabel.Visibility = Visibility.Visible;
                        grid_welcome.Visibility = Visibility.Hidden;
                        Switcher.Switch(new ExercisePage());
                    }));
                }
            }
            else if (e.Index == FORCE)
            {
                value_force = e.Value;
                if (value_force > 10)
                {
                    isSitting = true;
                    this.Dispatcher.Invoke(new Action(delegate()
                    {
                        grid_welcome.Visibility = Visibility.Visible;
                    }));
                }
            }
        }


        void Closing()
        {
            ifk.SensorChange -= new SensorChangeEventHandler(ifk_SensorChange);
            ifk.Error -= new ErrorEventHandler(ifk_Error);
            ifk.Detach -= new DetachEventHandler(ifk_Detach);
            ifk.Attach -= new AttachEventHandler(ifk_Attach);
        }
    
        public void UtilizeState(object state)
        {
 	        throw new NotImplementedException();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Switcher.Switch(new ExercisePage());
        }

        private void GoTo_Finish(object sender, RoutedEventArgs e)
        {
            Switcher.Switch(new FinishPage(), null);
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            Closing();
        }
    }
}
